package com.cv.system.api.dict;

import cn.hutool.core.util.StrUtil;
import com.cv.framework.common.enums.CommonStatusEnum;
import com.cv.framework.common.utils.object.BeanUtils;
import com.cv.system.api.dict.dto.DictDataRespDTO;
import com.cv.system.dal.dataobject.dict.DictDataDO;
import com.cv.system.service.dict.DictDataService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;

/**
 * 字典数据 API 接口实现类
 *
 * @author Charles_XDXD
 */
@DubboService
@Service
public class DictDataApiImpl implements DictDataApi {

    @Resource
    private DictDataService dictDataService;

    @Override
    public void validateDictDataList(String dictType, Collection<String> values) {
        dictDataService.validateDictDataList(dictType, values);
    }

    @Override
    public DictDataRespDTO getDictData(String dictType, String value) {
        DictDataDO dictData = dictDataService.getDictData(dictType, value);
        return BeanUtils.toBean(dictData, DictDataRespDTO.class);
    }

    @Override
    public DictDataRespDTO parseDictData(String dictType, String label) {
        DictDataDO dictData = dictDataService.parseDictData(dictType, label);
        return BeanUtils.toBean(dictData, DictDataRespDTO.class);
    }

    @Override
    public Collection<DictDataRespDTO> getDictDataListByType(String type) {
        if (StrUtil.isEmpty(type)) {
            return Collections.emptyList();
        }
        
        return dictDataService.getDictDataList(CommonStatusEnum.ENABLE.getStatus(), type)
                .stream()
                .map(dictData -> BeanUtils.toBean(dictData, DictDataRespDTO.class))
                .toList();
    }

}
